/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.omega_r.omegarecyclerview.pagination_example;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * ProgressBarExt类
 *
 * @since 2021-06-10
 */
public class ProgressBarExt extends Component implements Component.DrawTask {
    private static final int MINSWEEPANGLE = 20;
    private static final int MAXSWEEPANGLE = 300;
    private static final int MWIDTHRATE = 10;
    private static final int NUMONE = -1;
    private static final int NUMTHOUSAND = 1000;
    private static final int NUMTEN = 10;
    private static final int NUMFIVE = 5;
    private static final int NUMTWO = 2;
    private static final int THREEHUNDREDANDSIXTY = 360;
    private static final int NUMVALUE = 0xFF008577;
    private int mStartAngle = 0;
    private int mSweepAngle = MAXSWEEPANGLE;
    private int mType = 1;
    private RectFloat mRect;
    private Paint mPaint;
    private Arc mArc;

    /** 构造函数ProgressBarExt
     *
     * @param context
     */
    public ProgressBarExt(Context context) {
        super(context);
        init(context);
    }

    /** 构造函数ProgressBarExt
     *
     * @param context
     * @param attrSet
     */
    public ProgressBarExt(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    /** 构造函数ProgressBarExt
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public ProgressBarExt(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    /** 构造函数ProgressBarExt
     *
     * @param context
     * @param attrSet
     * @param resId
     */
    public ProgressBarExt(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint();
        mArc = new Arc();
        mPaint.setColor(new Color(NUMVALUE));
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        initAnimator(context);
        this.addDrawTask(this::onDraw);
    }

    private void initAnimator(Context context) {
        AnimatorValue animatorValue = new AnimatorValue();
        animatorValue.setCurveType(Animator.CurveType.LINEAR);
        animatorValue.setLoopedCount(NUMONE);
        animatorValue.setDuration(NUMTHOUSAND);
        animatorValue.setValueUpdateListener((animatorValue1, value) -> {
            if (mType == 1) {
                mStartAngle += NUMTEN;
                mSweepAngle -= NUMFIVE;
            }
            else {
                mStartAngle += NUMFIVE;
                mSweepAngle += NUMTEN;
            }
            mStartAngle %= THREEHUNDREDANDSIXTY;
            if (mSweepAngle <= MINSWEEPANGLE) {
                mType = NUMTWO;
            }
            else if (mSweepAngle >= MAXSWEEPANGLE) {
                mType = 1;
            }
            invalidate();
        });
        context.getUITaskDispatcher().asyncDispatch(() -> animatorValue.start());
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mRect == null) {
            int strokeWidth = component.getWidth() / MWIDTHRATE;
            mRect = new RectFloat(strokeWidth,
                strokeWidth,
                component.getWidth() - strokeWidth,
                component.getHeight() - strokeWidth);
            mPaint.setStrokeWidth(strokeWidth);
        }
        mArc.setArc(mStartAngle, mSweepAngle, false);
        canvas.drawArc(mRect, mArc, mPaint);
    }
}
